package imply;

import java.sql.*;
import java.util.*;

import Dao.HospitalDao;
import MyBean.Charge;
import MyBean.ChuFangDrug;
import MyBean.Message;
import MyBean.Program;
import MyBean.User;
import MyBean.patient;

public class HospitalDaoImply implements HospitalDao {
    private Connection conn;
    private PreparedStatement ps;
    private PreparedStatement ps1;

    public HospitalDaoImply(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean insertPatient(patient patient1) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "insert into patient(admissionNumber,patientName,sex,age,patientNumber,phone,roomno,startTime) values(?,?,?,?,?,?,?,?)";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, patient1.getAdmissionNumber());
            this.ps.setString(2, patient1.getPatientName());
            this.ps.setString(3, patient1.getSex());
            this.ps.setString(4, patient1.getAge());
            this.ps.setString(5, patient1.getPatientNumber());
            this.ps.setString(6, patient1.getPhone());
            this.ps.setString(7, patient1.getRoomno());
            this.ps.setString(8, patient1.getStartTime());
            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }

    @Override
	public boolean insertProgram(Program program) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			this.ps = this.conn.prepareStatement("insert into program values(?,?,?)");
			this.ps.setString(1,program.getAdmissionNumber());
			this.ps.setString(2,program.getProgramName());
			this.ps.setFloat(3,program.getProgramPrice());
			this.ps1 = this.conn.prepareStatement("insert into charge values(?,?,?)");
			this.ps1.setString(1,program.getAdmissionNumber());
			this.ps1.setString(2,program.getProgramName());
			this.ps1.setFloat(3,program.getProgramPrice());
			if(this.ps.executeUpdate()>0&&this.ps1.executeUpdate()>0)
			{
				flag = true;
			}
			this.ps.close();
			this.ps1.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
		
	}

    @Override
    public patient searchPatientById(String admissionNumber) {
        // TODO Auto-generated method stub
        patient patient1 = null;
        String sql = "select * from patient where admissionNumber=?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            ResultSet rs = this.ps.executeQuery();
            if (rs.next()) {
                patient1 = new patient(rs.getString("admissionNumber"), rs.getString("patientName"), rs.getString("sex"), rs.getString("age"), rs.getString("patientNumber"), rs.getString("phone"),rs.getString("roomno"), rs.getString("startTime"), rs.getString("endTime"));
            }
            rs.close();
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return patient1;
    }

    @Override
    public List<patient> findPatients(String keyWord) {
        // TODO Auto-generated method stub
        List<patient> list = new ArrayList<patient>();
        String sql = "select * from patient where patientName like ?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, "%" + keyWord + "%");
            ResultSet rs = this.ps.executeQuery();
            patient patient1 = null;
            while (rs.next()) {
                patient1 = new patient();
                patient1.setAdmissionNumber(rs.getString("admissionNumber"));
                patient1.setPatientName(rs.getString("patientName"));
                patient1.setSex(rs.getString("sex"));
                patient1.setAge(rs.getString("age"));
                patient1.setPatientNumber(rs.getString("patientNumber"));
                patient1.setPhone(rs.getString("phone"));
                patient1.setRoomno(rs.getString("roomno"));
                patient1.setStartTime(rs.getString("startTime"));
                patient1.setEndTime(rs.getString("endTime"));
                list.add(patient1);
            }
            rs.close();
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<Program> searchProgramById(String admissionNumber) {
        // TODO Auto-generated method stub
        List<Program> list = new ArrayList<Program>();
        String sql = "select * from program where admissionNumber=? ";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            ResultSet rs = this.ps.executeQuery();
            Program program1 = null;
            while (rs.next()) {
                program1 = new Program();
                program1.setAdmissionNumber(rs.getString("admissionNumber"));
                program1.setProgramName(rs.getString("programName"));
                program1.setProgramPrice(rs.getFloat("programPrice"));
                list.add(program1);
            }
            rs.close();
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<Charge> searchChargeById(String admissionNumber) {
        // TODO Auto-generated method stub
        List<Charge> list = new ArrayList<Charge>();
        String sql = "select * from charge where admissionNumber=? ";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            ResultSet rs = this.ps.executeQuery();
            Charge charge1;
            while (rs.next()) {
                charge1 = new Charge();
                charge1.setAdmissionNumber(rs.getString("admissionNumber"));
                charge1.setItemName(rs.getString("itemName"));
                charge1.setItemPrice(rs.getString("itemPrice"));
                list.add(charge1);
            }
            rs.close();
            this.ps.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public boolean UpdatePatient(patient patient1) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "update patient set patientName=?,sex=?,age=?,patientNumber=?,phone=?,roomno=?,startTime=?,endTime=? where admissionNumber=?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(9, patient1.getAdmissionNumber());
            this.ps.setString(1, patient1.getPatientName());
            this.ps.setString(2, patient1.getSex());
            this.ps.setString(3, patient1.getAge());
            this.ps.setString(4, patient1.getPatientNumber());
            this.ps.setString(5, patient1.getPhone());
            this.ps.setString(6, patient1.getRoomno());
            this.ps.setString(7, patient1.getStartTime());
            this.ps.setString(8, patient1.getEndTime());

            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }

    @Override
    public boolean deletePatient(String admissionNumber) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "delete from patient where admissionNumber=? ";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }

    @Override
    public boolean deleteProgram(String admissionNumber) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "delete from program where admissionNumber=?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteCharge(String admissionNumber) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "delete from charge where admissionNumber=?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }

    @Override
	public boolean insertChuFangDrug(ChuFangDrug cfd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		String sql1 = "insert into ChuFangDrug values(?,?,?,?)";
		String sql2 = "insert into charge values(?,?,?)";
		try {
			this.ps = this.conn.prepareStatement(sql1);
			this.ps.setString(1,cfd.getAdmissionNumber());
			this.ps.setString(2,cfd.getDrugName());
			this.ps.setFloat(3,cfd.getDrugPrice());
			this.ps.setString(4,cfd.getDosage());
			this.ps1 = this.conn.prepareStatement(sql2);
			this.ps1.setString(1,cfd.getAdmissionNumber());
			this.ps1.setString(2,cfd.getDrugName());
			this.ps1.setFloat(3,cfd.getDrugPrice());
			if(this.ps.executeUpdate()>0&&this.ps1.executeUpdate()>0)
			{
				flag = true;
			}
			this.ps.close();
			this.ps1.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}

    @Override
    public List<ChuFangDrug> searchChuFangDrugById(String admissionNumber) {
        // TODO Auto-generated method stub
        List<ChuFangDrug> list = new ArrayList<ChuFangDrug>();
        String sql = "select * from ChuFangDrug where admissionNumber=?";

        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            ResultSet rs = this.ps.executeQuery();
            while (rs.next()) {
                ChuFangDrug cfd = new ChuFangDrug();
                cfd.setAdmissionNumber(rs.getString("admissionNumber"));
                cfd.setDrugName(rs.getString("drugName"));
                cfd.setDrugPrice(rs.getFloat("drugPrice"));
                cfd.setDosage(rs.getString("dosage"));
                list.add(cfd);
            }
            rs.close();
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteChuFangDrug(String admissionNumber) {
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "delete from ChuFangDrug where admissionNumber =?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, admissionNumber);
            if (this.ps.executeUpdate() > 0) {
                flag = true;
            }
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return flag;
    }

    @Override
    public boolean findLogin(User user) throws Exception {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        boolean flag = false;
        try {
            String sql = "select * from user where UserId=? and UserPassword=?";
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, user.getUserId());
            this.ps.setString(2, user.getUserPassword());
            ResultSet rs = this.ps.executeQuery();
            if (rs.next()) {
                flag = true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw e;
        } finally {
            if (this.ps != null) {
                this.ps.close();
            }
        }
        return flag;


    }

    @Override
    public boolean findRegister(User user) throws Exception {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        boolean flag = false;
        String sql = "insert into user values(?,?,?)";
        this.ps = this.conn.prepareStatement(sql);
        this.ps.setString(1, user.getUserId());
        this.ps.setString(2, user.getUserPassword());
        this.ps.setString(3, user.getUserName());
        this.ps.executeUpdate();
        flag = true;
        this.ps.close();
        return flag;


    }
    @Override
    public Message searchMessageByName(String name) {
        // TODO Auto-generated method stub
        Message message = null;
        String sql = "select * from message where name=?";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.ps.setString(1, name);
            ResultSet rs = this.ps.executeQuery();
            if (rs.next()) {
                message = new Message(rs.getString("name"), rs.getFloat("price"));
            }
            rs.close();
            this.ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return message;
    }

}
